<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>录入保险记录</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/layui/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/css/public.css" media="all" />
</head>
<body class="childrenBody">
<form class="layui-form" style="padding: 20px" method="post" id="insuranceForm" >
    <div class="layui-form-item">
        <label class="layui-form-label">车辆</label>
        <div class="layui-input-block">
            <select id="allCar"  lay-verify="required" lay-filter="Carcheck" name="car.carid">
                <option value=""></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">驾驶员</label>
        <div class="layui-input-block">
            <input type="text" id="dirvername"  required  lay-verify="required" autocomplete="off" class="layui-input">
            <input type="text" hidden="hidden" name="driver.driverid" id="driverid">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">保险公司</label>
        <div class="layui-input-block">
            <select id="btype"  lay-verify="required"  name="btype.btypeid">
                <option value=""></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">保险类型</label>
        <div class="layui-input-block">
            <select id="allInsurance"  lay-verify="required" name="insurance.iid">
                <option value=""></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">保险起始日期</label>
        <div class="layui-input-block">
            <input type="text" id="insurance_starttime" name="insurance_starttime"  required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">保险费用</label>
        <div class="layui-input-block">
            <input type="text" id="insuranceprice" name="insuranceprice"  required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" id="remark" name="remark"  required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="insuranceAdd" id="addBtn">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</form>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/layui/layui/layui.js"></script>
<script>
    layui.use(['form','layer','laydate','table','laytpl','upload'],function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laydate = layui.laydate,
            laytpl = layui.laytpl,
            table = layui.table,
            upload=layui.upload;

        //日期插件
        lay('#insurance_starttime').each(function(){
            laydate.render({
                elem: this
                ,format:'yyyy-MM-dd HH:mm:ss'
                ,type:'datetime'
                ,trigger: 'click'
            });
        });


        //获取car数据
        $.ajax({
            url:"/car/allcar",
            success:function (res) {
                for(var i=0;i<res.data.length;i++){
                    var op=$("<option></option>");
                    op.attr('value',res.data[i].carid);
                    op.append(res.data[i].carplate);
                    $("#allCar").append(op);
                }
                form.render('select');
            }
        });
        //获得保险类型
        $.ajax({
            url:"/insurance/insurances",
            success:function (res) {
                for(var i=0;i<res.data.length;i++){
                    console.log(res.data[i])
                    var op=$("<option></option>");
                    op.attr('value',res.data[i].iid);
                    op.append(res.data[i].itype);
                    $("#allInsurance").append(op);
                }
                form.render('select');
            }
        })
        //获取btype数据
        $.ajax({
            url:"/btype/allBtype",
            success:function (res) {
                for(var i=0;i<res.data.length;i++){
                    var op=$("<option></option>");
                    op.attr('value',res.data[i].btypeid);
                    op.append(res.data[i].btypename);
                    $("#btype").append(op);
                }
                form.render('select');
            }
        });
        //选定车辆后得到驾驶员
        form.on('select(Carcheck)',function (data) {
            var carid=data.value
            $.ajax({
                url:"/car/car",
                data:{carid:carid},
                success:function (res) {
                    $("#dirvername").attr("value",res.data.driver.dirvername);
                    $("#driverid").attr("value",res.data.driver.driverid);
                }
            })

        })


        //提交表单
        form.on("submit(insuranceAdd)",function(data) {
            $.ajax({
                type: "post",
                url: "/insuranceRecord/addInsuranceRecord",
                data:$('#insuranceForm').serialize(),
                success: function (res) {
                  layui.layer.msg(res.msg)
                }
            })
            //点击提交后关闭悬浮窗
            setTimeout(function () {
                window.parent.location.reload()
            },1000)
            return false;
        });

    })
</script>
</body>
</html>